package cn.com.czpb.client.module.news.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import cn.com.czpb.client.R;
import cn.com.czpb.client.app.ListBaseAdapter;
import cn.com.czpb.client.model.news.AreaChannelBean;
import cn.com.czpb.client.model.news.CityChannelBean;
import cn.com.czpb.client.utils.SPUtil;

public class CityDragAdapter extends ListBaseAdapter<AreaChannelBean> {
    private Activity activity;
    private List<CityChannelBean> resetList1;
    private List<CityChannelBean> resetList2;
    private int mHidePosition = -1;
    private int type;

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private boolean isEdit;

    public CityDragAdapter(Activity activity, List<AreaChannelBean> list, int type) {
        super(activity);
        this.activity = activity;
        this.list = list;
        this.type = type;
        isEdit = false;
        //reset();

    }

    public void setList(List<AreaChannelBean> titleData) {
        this.list = titleData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void appendData(List<AreaChannelBean> data, boolean isClearOld) {
        super.appendData(data, isClearOld);
        //getChoiceList();
    }

    boolean animFlag = false;

    public void setAnim(boolean animFlag) {
        this.animFlag = animFlag;
    }

    public void deleteDataAtPosition(int position) {
        list.remove(position);
        notifyDataSetChanged();
    }

    public void addDataAtPosition(AreaChannelBean ncb, int position) {
        if (position == -1) {
            list.add(ncb);
        } else {
            list.add(position, ncb);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int p = position;
        final AreaChannelBean ncb = list.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.city_channel_item, parent, false);
        }

        final TextView tv = convertView.findViewById(R.id.city_cancel_griditem_tv);
//        if (position == 0) {
//            tv.setBackgroundColor(context.getResources().getColor(R.color.white));
//        }
        if(type == 1 && position == 0){
            TextPaint tp = tv.getPaint();
            tp.setFakeBoldText(true);
//            tv.setClickable(false);
        }
        if(type == 0){
            TextPaint tp = tv.getPaint();
            tp.setFakeBoldText(true);
        }
        if (p == list.size() - 1 && animFlag) {
            convertView.setVisibility(View.INVISIBLE);
        } else {
            convertView.setVisibility(View.VISIBLE);
        }
        /*
        if (type == 0) {
			// 隐藏推荐选项按钮（始终处于选中状态）
			if ("0".equals(ncb.getTid())) {
				convertView.setVisibility(View.GONE);
				channelListDbTask.addChoice(ncb);
			}
		}*/
        tv.setText(ncb.getName());
        tv.setTag(position);
//        if(position == 0 && type == 0){
//            tv.setBackgroundResource(0);
//        }else{
//            tv.setBackgroundResource(R.drawable.channel_item);
//        }
        if(SPUtil.getInstance().getCurrentCountry() == null){
            if(SPUtil.getInstance().getCurrentCity().equals(list.get(position).getName())){
                tv.setTextColor(Color.RED);
                tv.setBackgroundResource(R.drawable.channel_item_selected);
            }
        }else{
            if(SPUtil.getInstance().getCurrentCountryPosition() == position && SPUtil.getInstance().getCurrentCountry().getChild().get(0).getName().equals(list.get(0).getName())){
                tv.setTextColor(Color.RED);
                tv.setBackgroundResource(R.drawable.channel_item_selected);
            }
        }
        return convertView;
    }

    public void reorderItems(int oldPosition, int newPosition) {
        AreaChannelBean temp = list.get(oldPosition);
        if (oldPosition < newPosition) {
            for (int i = oldPosition; i < newPosition; i++) {
                Collections.swap(list, i, i + 1);
            }
        } else if (oldPosition > newPosition) {
            for (int i = oldPosition; i > newPosition; i--) {
                Collections.swap(list, i, i - 1);
            }
        }
        list.set(newPosition, temp);
    }

    public void setHideItem(int hidePosition) {
        this.mHidePosition = hidePosition;
        notifyDataSetChanged();
    }


    public void reset() {
        notifyDataSetChanged();
        /*
		resetList1 = new ArrayList<>();
		resetList2 = new ArrayList<>();
		for (NewsChannelBean ncb : list) {
			if (channelListDbTask.isChoice(ncb.getTid()) && !"0".equals(ncb.getTid())) {
				resetList1.add(ncb);
			} else {
				resetList2.add(ncb);
			}
		}
		// list.clear();
		list = resetList1;
		list.addAll(resetList2);
		notifyDataSetChanged();*/
    }

    @Override
    public View getMyView(int position, View convertView, ViewGroup parent) {

        return null;
    }

}
